﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LunoCode.Render
{
	class DuelPlace : BaseObject
	{
		Model model;
		VertexBuffer grid;

		public DuelPlace(GraphicsDevice device, Vector3 position, Vector3 rotation)
		{
			this.Position = position;
			this.Rotation = rotation;
            this.Scale = new Vector3(1.3e3f, 1.3e3f, 1.3e3f);

			grid = new VertexBuffer(device, typeof(VertexPositionColor), 22, BufferUsage.WriteOnly);

            VertexPositionColor[] points = new VertexPositionColor[22];
			points.Initialize();
			for (int i = 0; i < 18; i += 2)
			{
				points[i].Position = new Vector3(-0.5f, 0, (i-9)*0.5f);
				points[i + 1].Position = new Vector3(0.5f, 0, (i-9)*0.5f);
			}
			points[18].Position = new Vector3(-0.5f, 0, -4.5f);
			points[19].Position = new Vector3(-0.5f, 0, 3.5f);
			points[20].Position = new Vector3(0.5f, 0, -4.5f);
			points[21].Position = new Vector3(0.5f, 0, 3.5f);
			grid.SetData(points);

			model = LoadHelper.Models[ModelEnum.Lunohod];

			//foreach (ModelMesh mesh in model.Meshes)
			//    foreach (BasicEffect currenteffect in mesh.Effects)
			//        texture = currenteffect.Texture;
			foreach (ModelMesh modmesh in model.Meshes)
				foreach (ModelMeshPart modmeshpart in modmesh.MeshParts)
					modmeshpart.Effect = LoadHelper.Effects[EffectEnum.Phong].Clone(device);
		}

		public void Draw(GraphicsDevice device, Matrix view, Matrix proj)
		{
            Effect effect = LoadHelper.Effects[EffectEnum.Basic];
			effect.CurrentTechnique = effect.Techniques[0];

			effect.Parameters["wvp"].SetValue(Matrix.CreateScale(Scale) 
												* RotationMatrix
												* Matrix.CreateTranslation(Position) 
												* view 
												* proj);

			effect.Parameters["Color"].SetValue(Vector4.One);

			effect.Begin(); 
			foreach (EffectPass pass in effect.CurrentTechnique.Passes)
			{
				pass.Begin();

				device.Vertices[0].SetSource(grid, 0, VertexPositionColor.SizeInBytes);
				device.VertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);

				device.DrawPrimitives(PrimitiveType.LineList, 0, 11);

				pass.End();
			}
			effect.End();

			foreach (ModelMesh modmesh in model.Meshes)
			{
				foreach (Effect cureffect in modmesh.Effects)
				{
					cureffect.CurrentTechnique = cureffect.Techniques[0];

					cureffect.Parameters["WvpXf"].SetValue(World * view * proj);
					cureffect.Parameters["WorldXf"].SetValue(World);
					Matrix wIT = Matrix.CreateScale(Scale)
										 * Matrix.CreateRotationX(-MathHelper.PiOver2)
										 * RotationMatrix
										 * Matrix.Invert(Matrix.CreateTranslation(Position));
					cureffect.Parameters["WorldITXf"].SetValue(wIT);
					cureffect.Parameters["ViewIXf"].SetValue(Matrix.Invert(view));
					cureffect.Parameters["Lamp0Pos"].SetValue(new Vector3(30681.61f, 29085.93f, -51236.8f));
					//cureffect.Parameters["ColorTexture"].SetValue(LoadHelper.Textures[TextureEnum.Moon]);
					//cureffect.Parameters["Ks"].SetValue(0.4f);
				}
				modmesh.Draw();
			}
		}
	}
}
